//
//  RBCategoryTagView.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/13.
//

#import "RBCategoryTagView.h"
#import "RBTagContainerView.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import <Masonry.h>
#import "RanTool.h"
#import <UIView+YYAdd.h>
#import <UIColor+YYAdd.h>

#define RBCategoryTagItemViewNoti @"RBCategoryTagItemViewNoti"

@interface RBCategoryTagModel:NSObject
@property(nonatomic, copy)NSString *title;
@property(nonatomic, assign)BOOL selected;
@property(nonatomic, assign) NSInteger index;
@end
@implementation RBCategoryTagModel
@end

@interface RBCategoryTagItemView : UIView
@property(nonatomic, strong)RBCategoryTagModel *model;
+ (CGFloat)tagItemHeight;
@property(nonatomic, strong)UILabel *titleL;
@end
@implementation RBCategoryTagItemView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.titleL = UILabel.new;
        self.titleL.textAlignment = NSTextAlignmentCenter;
        self.titleL.font = [UIFont systemFontOfSize:Zoom(13)];
        [self addSubview:self.titleL];
        [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(Zoom(16));
            make.right.mas_equalTo(-Zoom(16));
        }];
        [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(reloadEvent) name:RBCategoryTagItemViewNoti object:nil];
    }
    return self;
}

- (void)dealloc
{
    [NSNotificationCenter.defaultCenter removeObserver:self];
}

- (void)reloadEvent
{
    [self setModel:self.model];
}

- (void)setModel: (RBCategoryTagModel *)model
{
    _model = model;
    self.titleL.text = model.title;
    if (model.selected)
    {
        self.titleL.textColor = UIColor.whiteColor;
        self.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
    }
    else
    {
        self.titleL.textColor = [UIColor colorWithHexString:@"#518CFF"];
        self.backgroundColor = UIColor.whiteColor;
    }
    CGFloat width = [RanTool sizeWithText:model.title consSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:self.titleL.font].width + 2 + Zoom(16)*2;
    width = MAX(width, Zoom(81));
    self.width = width;
    self.height = [self.class tagItemHeight];
    [self rbSetCornerRadius:self.height/2];
    [self rbSetBorderWitdh:1 borderColor:[UIColor colorWithHexString:@"#518CFF"]];
}
+ (CGFloat)tagItemHeight
{
    return Zoom(28);
}
@end

@interface RBCategoryTagView()
@property(nonatomic, strong)NSArray *models;
@end

@implementation RBCategoryTagView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
       
    }
    return self;
}

#pragma mark - System

- (void)setTags: (NSArray *)tempTags
{
    NSArray *tags = ({
        NSMutableArray *arrayM = NSMutableArray.array;
        for(NSInteger i = 0; i < tempTags.count; i++)
        {
            RBCategoryTagModel *model = RBCategoryTagModel.new;
            model.title = tempTags[i];
            model.selected = !i;
            model.index = i;
            [arrayM addObject:model];
        }
        arrayM;
    });
    self.models = tags;
    
    CGFloat padding = Zoom(13);
    CGFloat vMargin = Zoom(13);
    CGFloat hMargain = Zoom(16);
    NSArray *tagViews = ({
        NSMutableArray *arrayM = NSMutableArray.array;
        for (RBCategoryTagModel *model in tags)
        {
            RBCategoryTagItemView *itemView = RBCategoryTagItemView.new;
            [itemView rbAddTapGesWithTarget:self action:@selector(tagItemTapped:)];
            [itemView setModel:model];
            [arrayM addObject:itemView];
        }
        arrayM;
    });
    
    RBTagContainerView *tagContainer = RBTagContainerView.new;
    tagContainer.vMargin = vMargin;
    tagContainer.hMargin = hMargain;
    tagContainer.constraintWidth = kRBMainScreenWidth - padding*2;
    [tagContainer addTagViews:tagViews];
    if (tagContainer.calulatedHeight < [RBCategoryTagItemView tagItemHeight]*2 + vMargin + 8)
    {
        [self addSubview:tagContainer];
        [tagContainer mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(padding);
            make.right.mas_equalTo(-padding);
            make.top.bottom.mas_equalTo(0);
            make.height.mas_equalTo(tagContainer.calulatedHeight);
        }];
    }
    else
    {
        CGFloat topX = 0;
        CGFloat bottomX = 0;
        CGFloat width = kRBMainScreenWidth;
        CGFloat height = [RBCategoryTagItemView tagItemHeight]*2 + vMargin;
        UIScrollView *scrollView = UIScrollView.new;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:scrollView];
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(height);
            make.width.mas_equalTo(width);
            make.edges.mas_equalTo(0);
        }];
        for (UIView *view in tagViews)
        {
            [scrollView addSubview:view];
            if (topX > bottomX)
            {
                if (!bottomX)
                {
                    bottomX += padding;
                }
                else
                {
                    bottomX += hMargain;
                }
                view.left = bottomX;
                bottomX += view.width;
                view.top = [RBCategoryTagItemView tagItemHeight] + vMargin;
            }
            else
            {
                if (!topX)
                {
                    topX += padding;
                }
                else
                {
                    topX += hMargain;
                }
                view.left = topX;
                topX += view.width;
                view.top = 0;
            }
        }
        CGFloat maxW = MAX(topX, bottomX) + padding;
        scrollView.contentSize = CGSizeMake(maxW, height);
    }
}

#pragma mark - Event

- (void)tagItemTapped: (UITapGestureRecognizer *)ges
{
    RBCategoryTagItemView *view = (RBCategoryTagItemView *)ges.view;
    RBCategoryTagModel *model = view.model;
    BOOL multiSelect = NO;
    BOOL canDeselect = NO;
    if (model.selected)
    {
        if (canDeselect)
        {
            model.selected = !model.selected;
        }
        else
        {
            return;
        }
    }
    else
    {
        if (!multiSelect)
        {
            for (RBCategoryTagModel *itemModel in self.models)
            {
                itemModel.selected = NO;
            }
            
        }
        if(self.selectBlock)
        {
            self.selectBlock(model.index);
        }
        model.selected = YES;
    }
    [NSNotificationCenter.defaultCenter postNotificationName:RBCategoryTagItemViewNoti object:nil];
}

@end
